<?php

/**
 * 获取列表
 * @param  integer
 * @param  integer
 * @return [type]
 */
function page_model_get_list($page = 1, $page_size = 8) {
	$page = intval($page);
	$limit = ($page - 1) * $page_size;

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from pages where `status` = 1 and `draft` =0  order by is_top DESC,publish_time DESC limit " . $limit . "," . $page_size;
	$pages_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			while ($row = mysqli_fetch_assoc($result)) {
				//获取菜单信息
				$menu = page_helper_get_menu($row['menu_id']);
				//菜单信息赋值
				//菜单名称
				$row['menu_name'] = $menu['menu_name'];
				//菜单路径
				$row['menu_url'] = $menu['menu_path'];
				$pages_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $pages_array;
}
/**
 * 获取列表
 * @param  integer
 * @param  integer
 * @return [type]
 */
function page_model_get_unbuild_list() {
	$page_size = 20;
	$page = 1;
	$limit = ($page - 1) * $page_size;

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	//查询未生成HTML的文章以及文章生成时间大于1个小时的。
	$sql = "select * from pages where `status` = 1 and `draft` =0 and (`is_build`=0 or timestampdiff(hour, build_time, CURRENT_TIMESTAMP)>1 )limit " . $limit . "," . $page_size;
	$pages_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			while ($row = mysqli_fetch_assoc($result)) {
				//获取菜单信息
				$menu = page_helper_get_menu($row['menu_id']);
				//菜单信息赋值
				//菜单名称
				$row['menu_name'] = $menu['menu_name'];
				//菜单路径
				$row['menu_url'] = $menu['menu_path'];
				$pages_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $pages_array;
}
/**
 * 根据菜单获取列表
 * @param  integer
 * @param  integer
 * @param  integer
 * @return [type]
 */
function page_model_get_list_by_menu_id($menu_id = 0, $page = 1, $page_size = 8, $keywords = '') {
	//数据安全过滤
	//菜单
	$menu_id = intval($menu_id);
	//分页
	$page = intval($page);
	if ($page <= 0) {
		$page = 1;
	}
	//页码转换成数据库limit
	$limit = ($page - 1) * $page_size;

	require_once APP_PATH . DIRECTORY_SEPARATOR . 'menu/model.php';
	//获取下级所有菜单ID
	$menud_ata = menu_model_get_by_id($menu_id);

	$menu_ids = isset($menud_ata) ? $menud_ata['childs'] : '';
	//菜单ID通过,链接
	if (empty($menu_child_ids)) {
		$menu_ids = $menu_id;
	} else {
		$menu_ids = $menu_child_ids . ',' . $menu_id;
	}

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	//sql查询
	$sql = "select * from pages where menu_id in(" . $menu_ids . ") and`status` = 1 and `draft` =0    order by id desc limit " . $limit . "," . $page_size;
	//结果数组
	$pages_array = array();
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			//循环结果
			while ($row = mysqli_fetch_assoc($result)) {
				//获取菜单信息
				$menu = page_helper_get_menu($row['menu_id']);
				//菜单信息赋值
				//菜单名称
				$row['menu_name'] = $menu['menu_name'];
				//菜单路径
				$row['menu_url'] = $menu['menu_path'];
				//加入结果
				$pages_array[] = $row;
			}
		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回数组
	return $pages_array;
}

function page_model_get_list_count_by_menu_id($menu_id = 0, $keywords = '') {
	//数据安全过滤
	//菜单
	$menu_id = intval($menu_id);
	require_once APP_PATH . DIRECTORY_SEPARATOR . 'menu/model.php';
	//获取下级所有菜单ID
	$menu_child_ids = menu_model_get_child_ids($menu_id);
	$menu_ids = "";
	//菜单ID通过,链接
	if (empty($menu_child_ids)) {
		$menu_ids = $menu_id;
	} else {
		$menu_ids = $menu_child_ids . ',' . $menu_id;
	}

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	//sql查询
	$sql = "select count(id) as count from pages where menu_id in(" . $menu_ids . ") and `status` = 1 and `draft` =0   order by id desc ";
	$result_count = 0;
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			$row = mysqli_fetch_assoc($result);
			if ($row) {
				$result_count = $row['count'];
			}

		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回
	return $result_count;
}

function page_model_get_all_list_count_by_menu_id($menu_id = 0, $keyword = '') {
	$where = " 1=1 ";

	//数据安全过滤
	$keyword = trim($keyword);
	//菜单
	$menu_id = intval($menu_id);
	require_once APP_PATH . DIRECTORY_SEPARATOR . 'menu/model.php';
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	if (!empty($menu_id)) {
		//获取下级所有菜单ID
		$menu_child_ids = menu_model_get_child_ids($menu_id);
		$menu_ids = "";
		//菜单ID通过,链接
		if (empty($menu_child_ids)) {
			$menu_ids = $menu_id;
		} else {
			$menu_ids = $menu_child_ids . ',' . $menu_id;
		}

		$where .= "and menu_id in (" . $menu_ids . ") ";
	}

	if (!empty($keyword)) {
		$where .= "and (title like '%" . $keyword . "%' or content like '%" . $keyword . "%')";
	}

	//sql查询
	$sql = "select count(id) as count from pages where " . $where;
	$result_count = 0;
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			$row = mysqli_fetch_assoc($result);
			if ($row) {
				$result_count = $row['count'];
			}

		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回
	return $result_count;
}
/**
 * 根据菜单获取列表
 * @param  integer
 * @param  integer
 * @param  integer
 * @return [type]
 */
function page_model_get_all_list_by_menu_id($menu_id = 0, $page = 1, $page_size = 8, $keyword = '') {
	$where = " 1=1 ";
	//数据安全过滤
	//菜单
	$menu_id = intval($menu_id);
	//分页
	$page = intval($page);
	//防止出现负数
	if ($page <= 0) {
		$page = 1;
	}
	//页码转换成数据库limit
	$limit = ($page - 1) * $page_size;
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';

	require_once APP_PATH . DIRECTORY_SEPARATOR . 'menu/model.php';

	if (!empty($menu_id)) {
		//获取下级所有菜单ID
		$menu_child_ids = menu_model_get_child_ids($menu_id);
		$menu_ids = "";
		//菜单ID通过,链接
		if (empty($menu_child_ids)) {
			$menu_ids = $menu_id;
		} else {
			$menu_ids = $menu_child_ids . ',' . $menu_id;
		}

		$where .= "and menu_id in (" . $menu_ids . ") ";
	}

	if (!empty($keyword)) {
		$where .= "and (title like '%" . $keyword . "%' or content like '%" . $keyword . "%')";
	}
	//sql查询
	$sql = "select * from pages where " . $where . "  order by id desc  limit " . $limit . "," . $page_size;
	//结果数组
	$pages_array = array();
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			//循环结果
			while ($row = mysqli_fetch_assoc($result)) {
				//获取菜单信息
				$menu = page_helper_get_menu($row['menu_id']);
				//菜单信息赋值
				//菜单名称
				$row['menu_name'] = $menu['menu_name'];
				//菜单路径
				$row['menu_url'] = $menu['menu_path'];

				$row['draft'] = ($row['draft'] == 1) ? '草稿' : '正常';
				$row['status'] = ($row['status'] == 1) ? '已发布' : '隐藏';
				$row['is_build'] = ($row['is_build'] == 1) ? '已生成HTML' : '未生成';
				//加入结果
				$pages_array[] = $row;
			}
		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回数组
	return $pages_array;
}

/**
 * 根据hash获取数据
 *
 * @param  string
 * @return [type]
 */
function page_model_get_by_hash($hash = '') {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from pages where `hash`='" . $hash . "' limit 1";
	$page_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			$page_data = mysqli_fetch_assoc($result);
			if (!empty($page_data)) {
				//获取菜单信息
				$menu = page_helper_get_menu($page_data['menu_id']);
				//菜单信息赋值
				//菜单名称
				$page_data['menu_name'] = $menu['menu_name'];
				//菜单路径
				$page_data['menu_url'] = $menu['menu_path'];
			} else {
				$page_data = false;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $page_data;
}

/**
 * 根据url获取数据
 *
 * @param  string
 * @return [type]
 */
function page_model_get_by_url($url = '') {
	if (empty($hash)) {
		return false;
	}
	$url = filter_data($url);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from pages where `url`='" . $url . "' limit 1";
	$page_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/page/helper.php';
			$page_data = mysqli_fetch_assoc($result);
			if (!empty($page_data)) {
				//获取菜单信息
				$menu = page_helper_get_menu($page_data['menu_id']);
				//菜单信息赋值
				//菜单名称
				$page_data['menu_name'] = $menu['menu_name'];
				//菜单路径
				$page_data['menu_url'] = $menu['menu_path'];
			} else {
				$page_data = false;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $page_data;
}

function page_model_insert_count($hash = '', $ip = '', $user_agent = '') {
	if (empty($hash)) {
		return false;
	}
	if (empty($ip)) {
		return false;
	}
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	$ip = trim($ip);
	$user_agent = fn_safe($user_agent);
	$create_time = date('Y/m/d H:i:s', time());

	$sql = "insert into page_counts(hash,ip,user_agent,create_time)values('" . $hash . "','" . $ip . "','" . $user_agent . "','" . $create_time . "')";
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);
	} catch (Exception $e) {
		// TODO
	}
}

function page_model_insert($url = '', $title = '', $menu_id = 0, $content = '', $status = 0, $draft = 0, $keywords = '', $description = '') {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$title = trim($title);
	$publish_time = date('Y-m-d H:i:s', time());
	$hash = md5($publish_time);
	$draft = intval($draft);
	$status = intval($status);

	$keywords = trim($keywords);
	$description = trim($description);
	//SQL模板
	$sql = "insert into pages(url,hash,menu_id,title,content,publish_time,draft,status,keywords,description)values(?,?,?,?,?,?,?,?,?)";

	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			printf("MYSQL ERROR: %s\n", mysqli_error($db));
			return false;
		}

		if (!$stmt->bind_param('ssisssiiss', $url, $hash, $menu_id, $title, $content, $publish_time, $draft, $status, $keywords, $description)) {

			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			return true;
		}

	} catch (Exception $e) {
		return false;
		// TODO
		echo $e;
	}
}

function page_model_update_by_hash($url = '', $hash = '', $title, $menu_id = 0, $content = '', $status = 0, $draft = 0, $keywords = '', $description = '') {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}

	$title = trim($title);
	$modify_time = date('Y-m-d H:i:s', time());
	$draft = intval($draft);
	$status = intval($status);
	$sql = "update pages set url=?,menu_id=?,title=?,content=?,modify_time=?,draft=?,status=?,keywords=?,description=? where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			printf("MYSQL ERROR: %s\n", mysqli_error($db));
			return false;
		}

		if (!$stmt->bind_param('sisssiisss', $url, $menu_id, $title, $content, $modify_time, $draft, $status, $keywords, $description, $hash)) {

			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			require_once APP_PATH . '/page/build.php';
			//更新HTML
			page_build_page_by_hash($hash);
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
	}
}
/**
 * 根据hash获取数据
 *
 * @param  string
 * @return [type]
 */
function page_model_get_count_by_hash($hash = '') {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select page_view_count from pages where `hash`='" . $hash . "' limit 1";
	$page_view_count = 0;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			$page_view_data = mysqli_fetch_assoc($result);
			if ($page_view_data) {
				$page_view_count = $page_view_data['page_view_count'];
			}
		} else {
			$page_view_count = 0;
		}

	} catch (Exception $e) {
		// TODO
	}
	return $page_view_count;
}

/**
 * 根据hash更新统计信息
 *
 * @param  string
 * @return [type]
 */
function page_model_update_count_by_hash($hash = '') {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "update pages set page_view_count=page_view_count+1  where `hash`='" . $hash . "'";
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);
	} catch (Exception $e) {
		// TODO
	}
}
/**
 * 根据hash更新统计信息
 *
 * @param  string
 * @return [type]
 */
function page_model_update_is_build_by_hash($hash = '', $is_build = 1) {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	$is_build = intval($is_build);
	$build_time = date('Y-m-d H:i:s', time());
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "update pages set is_build='" . $is_build . "' ,build_time='" . $build_time . "'  where `hash`='" . $hash . "'";
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	} catch (Exception $e) {
		// TODO
		return false;
	}
	return false;
}
function page_model_delete_by_hash($hash = '') {
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}
	$page_data = page_model_get_by_hash($hash);

	//删除数据库记录
	$sql = "delete from pages where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {

			return false;
		}

		if (!$stmt->bind_param('s', $hash)) {

			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			require_once APP_PATH . '/page/build.php';
			//删除物理文件
			page_delete_html_by_hash($page_data);
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
		return false;
	}
}